package com.example.service.tribune;


import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.exception.CustomException;
import com.example.mapper.tribune.CollectMapper;
import com.example.mapper.tribune.PostMapper;
import com.example.model.entity.other.Account;
import com.example.model.entity.tribune.Collect;
import com.example.model.vo.PostVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 收藏信息表业务处理
 **/
@Service
public class CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    private PostMapper postMapper;

    /**
     * 新增
     */
    public void add(Collect collect) {
        collectMapper.insert(collect);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        collectMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            collectMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Collect collect) {
        collectMapper.updateById(collect);
    }

    /**
     * 根据ID查询
     */
    public Collect selectById(Integer id) {
        return collectMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Collect> selectAll(Collect collect) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            collect.setUserId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            collect.setDoctorId(currentUser.getId());
        }
        return collectMapper.selectAll(collect);
    }

    /**
     * 分页查询
     */
    public PageInfo<Collect> selectPage(Collect collect, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            collect.setUserId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            collect.setDoctorId(currentUser.getId());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Collect> list = collectMapper.selectAll(collect);
        return PageInfo.of(list);
    }

    public void save(Collect collect) {
        Account currentUser = TokenUtils.getCurrentUser();
        Collect dbCollect=new Collect();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            collect.setUserId(currentUser.getId());
            dbCollect = collectMapper.selectByUserIdAndFid(collect.getUserId(), collect.getFid(),collect.getModule());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            collect.setDoctorId(currentUser.getId());
            dbCollect = collectMapper.selectByDoctorIdAndFid(collect.getDoctorId(), collect.getFid(),collect.getModule());
        }
        if (dbCollect != null) {  // 说明收藏过了
            collectMapper.deleteById(dbCollect.getId());  //删除收藏
            if(collect.getModule().equals("POST")){
                PostVO postVO = postMapper.selectById(collect.getFid());
                Integer collectNum = postVO.getCollectNum();
                postVO.setCollectNum(collectNum-1);
                postMapper.updateById(postVO);
            }
            throw new CustomException(ResultCodeEnum.COLLECT_ALREADY_NO);
        } else {
            // 新的收藏
            collectMapper.insert(collect);
            if(collect.getModule().equals("POST")){
                PostVO postVO = postMapper.selectById(collect.getFid());
                Integer collectNum = postVO.getCollectNum();
                postVO.setCollectNum(collectNum+1);
                postMapper.updateById(postVO);
            }
        }
    }

    public Collect selectByUserId(Integer userId, Integer fid,String module) {
        Account currentUser = TokenUtils.getCurrentUser();
        Collect dbCollect=new Collect();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            dbCollect = collectMapper.selectByUserIdAndFid(userId, fid,module);
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            dbCollect = collectMapper.selectByDoctorIdAndFid(userId, fid,module);
        }
        return dbCollect;
    }
}
